package com.cn.initialize;

import java.util.Date;
import java.util.List;

import cn.com.allunion.common.DateModule;
import cn.com.allunion.common.UUIDGenerator;
import cn.com.allunion.sms.pojo.Authority;
import cn.com.allunion.sms.pojo.Userbase;
import cn.com.allunion.sms.pojo.UserbaseAuthority;
import cn.com.allunion.sms.service.mybatis.AuthorityService;
import cn.com.allunion.sms.service.mybatis.UserbaseAuthorityService;
import cn.com.allunion.sms.service.mybatis.UserbaseService;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.cn.common.RandomModule;

/**
 * 初始化程序
 *
 * @author Administrator
 */
public class Initializer {
    /**
     * 日志
     */
    Logger logger = Logger.getLogger(Initializer.class);


    /**
     * 依赖注入获得时间操作模块实例
     */
    public DateModule dateModule;

    /**
     * 依赖注入获得随机数操作模块实例
     */
    public RandomModule randomModule;

    AuthorityService authorityService;

    UserbaseService userbaseService ;

    UserbaseAuthorityService userbaseAuthorityService ;

    /**
     * 初始化abcd用户
     */
    public void initAbcd() {
        List<Authority> authorityList = authorityService.findAll() ;
        /**
         * abcd是全权限用户
         */
        Userbase abcd = new Userbase();
        abcd.setUserId(UUIDGenerator.generatorRandomUUID());
        abcd.setUserName("abcd");
        abcd.setPassWord("abcd");
        abcd.setCreateDate(new Date());
        abcd.setWritepermission(true);
        userbaseService.save(abcd) ;

        for (int i = 0; i < authorityList.size(); ++i) {
            UserbaseAuthority ua = new UserbaseAuthority();
            ua.setUserbaseAuthorityId(UUIDGenerator.generatorRandomUUID());
            ua.setUserId(abcd.getUserId());
            ua.setAuthorityId(authorityList.get(i).getAuthorityId());
            userbaseAuthorityService.save(ua);
        }
    }

    /**
     * initAuthority 初始化权限
     *
     * @return String
     */
    public String initAuthority() {
        Authority authority_city = new Authority();
        authority_city.setAuthorityId(UUIDGenerator.generatorRandomUUID());
        authority_city.setAuthorityString("ROLE_ADMIN");
        authority_city.setAuthorityDescription("超级管理员用户");
        authority_city.setAuthorityExpireDate(new Date());
        authority_city.setAuthorityStatus(0);

        authorityService.save(authority_city) ;

        Authority authority_school = new Authority();
        authority_school.setAuthorityId(UUIDGenerator.generatorRandomUUID());
        authority_school.setAuthorityString("ROLE_MANAGER");
        authority_school.setAuthorityDescription("管理员用户");
        authority_school.setAuthorityExpireDate(new Date());
        authority_school.setAuthorityStatus(0);
        authorityService.save(authority_school) ;

        return "initAuthority() Return Success";
    }

    public String InitSuperAdmin() {
        Userbase ub = new Userbase();
        ub.setUserId(UUIDGenerator.generatorRandomUUID());
        ub.setUserName("super");
        ub.setPassWord("123");
        ub.setEnabled(true);
        ub.setUserType("superAdmin");
        userbaseService.save(ub) ;

        Authority au = authorityService.findByAuthorityString("ROLE_ADMIN") ;
        UserbaseAuthority uba = new UserbaseAuthority();
        uba.setUserbaseAuthorityId(UUIDGenerator.generatorRandomUUID());
        uba.setUserId(ub.getUserId());
        uba.setAuthorityId(au.getAuthorityId());
        userbaseAuthorityService.save(uba) ;

        return "add super admin success";
    }

    /**
     * 初始化用户
     *
     * @param userNumber
     */
    public void initUser(int userNumber) {
        List<Authority> authorityList = authorityService.findAll() ;

        for (int i = 0; i < userNumber; ++i) {
            Userbase user = new Userbase();
            String username = randomModule.getRStr(RandomModule.myint_az, 3)
                    + randomModule.getRStr(RandomModule.myint_09, 3);
            user.setUserId(UUIDGenerator.generatorRandomUUID());
            user.setUserName(username);
            user.setPassWord(username);
            user.setWritepermission(randomModule.getRandomBoolean());

            userbaseService.save(user) ;

            UserbaseAuthority ua = new UserbaseAuthority();
            ua.setUserbaseAuthorityId(UUIDGenerator.generatorRandomUUID());
            ua.setUserId(user.getUserId());
            ua.setAuthorityId(authorityList.get(randomModule.getRandomInt(authorityList.size())).getAuthorityId());
            userbaseAuthorityService.save(ua) ;
        }
    }

    /**
     * 依赖注入获得时间操作模块实例
     */
    public DateModule getDateModule() {
        return dateModule;
    }

    public void setDateModule(DateModule dateModule) {
        this.dateModule = dateModule;
    }

    /**
     * 依赖注入获得随机数操作模块实例
     */
    public RandomModule getRandomModule() {
        return randomModule;
    }

    /**
     * 依赖注入获得随机数操作模块实例
     */
    public void setRandomModule(RandomModule randomModule) {
        this.randomModule = randomModule;
    }

    public AuthorityService getAuthorityService() {
        return authorityService;
    }

    public void setAuthorityService(AuthorityService authorityService) {
        this.authorityService = authorityService;
    }

    public UserbaseService getUserbaseService() {
        return userbaseService;
    }

    public void setUserbaseService(UserbaseService userbaseService) {
        this.userbaseService = userbaseService;
    }

    public UserbaseAuthorityService getUserbaseAuthorityService() {
        return userbaseAuthorityService;
    }

    public void setUserbaseAuthorityService(UserbaseAuthorityService userbaseAuthorityService) {
        this.userbaseAuthorityService = userbaseAuthorityService;
    }
}
